/**
 * Copyright (C) 2010, 2011 Neofonie GmbH
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the Apache License, Version 2.0
 * (the "License"); you may not use this file except in compliance with
 * the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package eu.dicodeproject.blogimport.importer;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.apache.log4j.PropertyConfigurator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.io.ClassPathResource;


/**
 * This main class is used to start the import process. It awaits an command
 * line parameter which can be GZ or XML. In case XML is used an polling xml
 * file source will be used, in case of GZ an one time running tar.gz file
 * source will be used.
 *  
 * 
 */

public final class ImportStarter {

	private static final Logger LOG = LoggerFactory.getLogger(ImportStarter.class);

	private static ImportRunner runner;

	private ImportStarter() {
		// no public c'tor needed
	}

	/**
	 * Starts the importer.
	 * 
	 * @param args
	 * @throws IOException
	 * @throws InterruptedException
	 * @throws ClassNotFoundException
	 */
	public static void main(String[] args) throws IOException {
		String importMode;
		if (args.length == 1) {
			importMode = args[0];
			if (importMode == null || importMode.length() < 1) {
				System.err.println("Please, check fileExtention arg[1]. Possible extentions are GZ, XML, RECOVER.");
				throw new IOException("Please, check fileExtention arg[1]. Possible extentions are GZ, XML, RECOVER.");
			}
		} else {
			System.err.println("Please, check fileExtention arg[0]. Possible extentions are GZ, XML, RECOVER.");
			throw new IOException(
					"Please, check arguments fileExtention arg[0]. Possible extentions are GZ, XML, RECOVER.");
		}

		try {
		  Properties logProperties = new Properties();
			logProperties.load(new FileInputStream(new ClassPathResource("eu.dicodeproject.blogimport.log4j.properties").getFile()));
			PropertyConfigurator.configure(logProperties);
		} catch (FileNotFoundException e) {
			System.err.println("Unable to configure log4j.");
			e.printStackTrace();
		}
		LOG.info("Starting application context.");
		final String[] paths = { "eu.dicodeproject.blogimport.context.xml" };
		final ApplicationContext appCtx = new ClassPathXmlApplicationContext(paths);

		if ("XML".equals(importMode)) {
			LOG.info("Using XML mode.");
			runner = (ImportRunner) appCtx.getBean("blogFileImport");
		} else if ("GZ".equals(importMode)) {
			LOG.info("Using GZ with publishdate mode.");
			runner = (ImportRunner) appCtx.getBean("blogGzImport");			
		} else if ("RECOVER".equals(importMode)) {
			LOG.info("Using RECOVER with publishdate mode.");
			runner = (ImportRunner) appCtx.getBean("blogRecoverFileImport");			
		} else {
			LOG.error("No matching mode found, aborting.");
			System.exit(-1);
		}

		LOG.info("Using " + runner.getSource().getClass());

		Runtime.getRuntime().addShutdownHook(new Thread() {
			@Override
			public void run() {
				LOG.info("Recieved shutdown request.");
				runner.requestStop();
				try {
					Thread.sleep(1000);
				} catch (InterruptedException e) {
					LOG.warn(e.getMessage());
				}
				LOG.info("Stopped server, shutting down.");
				// System.exit(0);
			}
		});
		LOG.info("Starting Import");
		runner.start();
	}

}
